package sistkaraoke.servlet.sala;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import sistkaraoke.modelo.IPersona;
import sistkaraoke.modelo.ISala;
import sistkaraoke.negocio.GestionSalas;
import sistkaraoke.negocio.IGestionSalas;

/**
 * Servlet implementation class ListarSalasServlet
 */
@WebServlet("/ListarSalasServlet")
public class ListarSalasServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListarSalasServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			
		try {

			HttpSession session = request.getSession();
			IPersona usuario_actual = (IPersona)session.getAttribute("USUARIO_ACTUAL");
			
			if (usuario_actual == null)
			{
				request.getRequestDispatcher("error.jsp").forward(request, response);
				return;
			}
			
			IGestionSalas negocio = new GestionSalas();
			
			request.setAttribute("salas_registradas", ((Collection<ISala>) negocio.listar(usuario_actual.getDni())));

			RequestDispatcher rd = request.getRequestDispatcher("ListarSalas.jsp");
			
			rd.forward(request, response);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

}
